<%--
  Created by IntelliJ IDEA.
  User: yh570
  Date: 2022/5/4
  Time: 23:15
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="../base.jsp" %>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${basePath}/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="${basePath}/css/public.css" media="all" type="text/css">
    <style>
        body{
            margin: 10px 0 0 0 !important;
        }
    </style>
</head>
<body>
<div class="layuimini-container">

    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">关键字</label>
                            <div class="layui-input-inline">
                                <input type="text" name="searchKey" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

<%--        <script type="text/html" id="toolbarDemo">--%>
<%--            <div class="layui-btn-container">--%>
<%--                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>--%>
<%--                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>--%>
<%--                <button class="layui-btn layui-btn-sm " lay-event="import"> 导入 </button>--%>
<%--            </div>--%>
<%--        </script>--%>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

<%--        <script type="text/html" id="currentTableBar">--%>
<%--            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>--%>
<%--            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>--%>
<%--            <a class="layui-btn layui-btn-xs layui-btn-warm " lay-event="reset">重置密码</a>--%>
<%--        </script>--%>

    </div>
</div>
<script src="${basePath}/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table','element'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            element=layui.element;
        ;

        table.render({
            elem: '#currentTableId',
            url: 'managerList/data',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [[
                {field: 'account',  title: '用户名', sort: true},
                {field: 'rolename',  title: '角色'},
                {field: 'email',  title: '电子邮件'}
            ]],
            limits: [2,5,10,15,20],
            limit: 5,
            page: true,
            skin: 'line',
            parseData:function (res){
                var result;
                if(this.page.curr){
                    result = res.data.slice(this.limit*(this.page.curr-1),this.limit*this.page.curr);
                }else{
                    result = res.data.slice(0,this.limit);
                }
                return{
                    "code":res.code,
                    "msg":res.msg,
                    "count":res.count,
                    "data":result
                };
            }
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchKey: data.field.searchKey
                }
            }, 'data');
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加管理员',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['500px', '200px'],
                    content: 'accountAdd.us?roleId=1',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId');
                var data = checkStatus.data;
                var ids='';
                for(var i=0;i<data.length;i++){
                    if(ids==''){
                        ids = data[i].id;
                    }else{
                        ids += ","+data[i].id;
                    }
                }
                if(ids==''){
                    layer.alert('请选择要删除的行');
                }else{
                    layer.confirm('真的删除这些行么', function (index) {
                        $.ajax({
                            url:'${basePath}/deleteUserBatch.us',
                            type:'get',
                            data:{'ids':ids},
                            dataType: "json",
                            success:function (res){
                                if(res.code===0){
                                    layer.msg(res.msg,function(){
                                        //重新加载页面
                                        table.reload('currentTableId', {
                                            page: {
                                                curr: 1
                                            }
                                        }, 'data');
                                    })
                                }else{
                                    layer.alert(res.msg);
                                }
                            }
                        })
                    })
                }
            }else if(obj.event === 'import'){
                var index = layer.open({
                    title: '文件上传',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: false,
                    area: ['100%', '100%'],
                    content: '${basePath}/uploadExcel.up',
                });
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {

                var index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'accountEdit.us?id='+data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    $.ajax({
                        url:'deleteUser.us',
                        data:{'id':data.id},
                        dataType:'json',
                        type:'get',
                        success:function (res){
                            if(res.code===0){
                                table.reload('currentTableId', {
                                    page: {
                                        curr: 1
                                    }
                                }, 'data');
                            }else {
                                layer.alert(res.msg);
                            }
                        }
                    })
                    obj.del();
                    layer.close(index);
                });
            }else if(obj.event === 'reset'){
                var email = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
                if(!email.test(data.email)){
                    layer.alert('邮箱格式不正确');
                    return false;
                }else {
                    layer.confirm('确定要重置密码吗',function (index){
                        $.ajax({
                            url:'resetPassword.us',
                            type:'get',
                            data:{'id':data.id,'email':data.email},
                            dataType: 'json',
                            success:function (res){
                                if(res.code===0){
                                    layer.msg(res.msg);
                                }else {
                                    layer.alert(res.msg);
                                }
                            }
                        })
                    })
                }
            }
        });

    });
</script>

</body>
</html>